package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.common.OssTemplate;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 套餐controller
 *
 * @author likeWen
 * @since 2022/11/20 10:09
 */
@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private OssTemplate ossTemplate;
    @Autowired
    private SetmealService setmealService;
    /**
     * 分页查询
     */
    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        log.info("[套餐-分页查询]data:{}", queryPageBean);
        //rpc查询数据
        PageResult pageResult = setmealService.pageQuery(queryPageBean);
        //封装返回结果
        return new Result(true, MessageConst.ACTION_SUCCESS, pageResult);
    }
    /**
     * 图片上传回显
     */
    @PostMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile multipartFile){
        log.info("文件上传,name:{},size:{}",multipartFile.getOriginalFilename(),multipartFile.getSize());
        String imagePath = null;
        try {
            imagePath = ossTemplate.upload(multipartFile.getOriginalFilename(), multipartFile.getInputStream());
        } catch (IOException e) {
            return new Result(false,MessageConst.PIC_UPLOAD_FAIL);
        }
        return new Result(true,MessageConst.PIC_UPLOAD_SUCCESS,imagePath);
    }
    /**
     * 套餐新增数据
     */
    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        log.info("[套餐-新增数据]data:{},ids:{}", setmeal,checkgroupIds);
        setmealService.add(setmeal,checkgroupIds);
        return new Result(true,MessageConst.ADD_SETMEAL_SUCCESS);
    }
}
